$(document).ready(function(){
	jQuery.validator.addMethod("alphanumeric",function( value, element,param ) {
		if(/[a-zA-Z0-9_]/.test(value))
		{
			return true;
		}else{
			return false;
		}
	},"用户名只能为数字或字母");
	
	jQuery.validator.addMethod("maximumvalue",function( value, element,param ) {
		if(parseInt($('#accessid').val())>16777216)
		{
			return false;
		}else{
			return true;
		}
	},"门禁卡号不能大于16777216");
	
	jQuery.validator.addMethod("checkPassword",function( value, element,param ) {
		if($("#txtPasswd").val() == ""){
			return true;
		}else{
			if(value.match(/(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])[A-Za-z0-9]{8,16}/) == null)
			{
				return false;
			}else{
				return true;
			}
		}
	},"密码长度大于8小于16，且包括大小写字母、数字");
	
	jQuery.validator.addMethod("checkPwd",function( value, element, param){
		if($("#txtPasswd").val() == ""){
			return true;
		}else{
			var checkPwd = $("#txtPasswd").val().toLowerCase();
			var checkUsername = $('#txtUsername').val().toLowerCase();
			if(checkPwd.match(checkUsername) == null){
				return true;
			}else{
				return false;
			}
		}
	},"密码不能包含用户名或用户名大小写变形");
	
	jQuery.validator.addMethod("checkIDCardNo",function( value, element, param){
		if($("#txtIDCardNo").val() == ""){
			return false;
		}else{
			if(value.match(/(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)/) == null){
				return false;
			}else{
				return true;
			}
		}
	},"身份证号码格式错误");
	
	$("form:eq(0)").validate({
        rules: {
        	txtUsername:{ 
        		alphanumeric:true,
        		//必填
        		required: $('#txtUsername').is(':enabled'),
        		//最短长度
        		minlength: 5,
        		remote:{
        			url: '/portal/checkaccount',
        			type: "post"
        		}
        	},
        	txtFullName: {
                required: true,
                minlength: 2
            },
            txtPasswd: {
                required: $('#txtUsername').is(':enabled'),
                checkPwd: true,
                checkPassword: true,
                minlength: 8,
                remote:{
                	url: '/portal/checkHistoryPwd',
                	type: "post",
                	data:{
                		username: function(){
                			return $("#txtUsername").val();
                		},
                		txtPasswd: function(){
                			return $("#txtPasswd").val();
                		}
                	}
                }
            },
            txtPasswdConfirm: {
            	//等于txtpasswd
            	equalTo: "#txtPasswd" 
            },
            txtMobile: {
                required: true,
                isMobile: true,
                remote:{
        			url: '/portal/checkphone',
        			type: "post",
        			data: {                     //要传递的数据
        		        username: function() {
        		            return $("#txtUsername").val();
        		        },
        		        txtMobile:function() {
        		            return $("#txtMobile").val();
        		        },	        
        		    }
        		}
            },
            txtEmail: {
                email: true,
                remote: {
                	url: "/portal/checkemail",     //后台处理程序
                	type: "post",
                	data: {                     //要传递的数据
        		        username: function() {
        		            return $("#txtUsername").val();
        		        },
        		        txtEmail:function() {
        		            return $("#txtEmail").val();
        		        },
        		    }
                }
            },
            accessid: {
            	maximumvalue:true,
             	digits:true,
                minlength: 10,
                maxlength: 20,
                remote: {
                	url: "/portal/checkaccessid",     //后台处理程序
                	type: "post",
                	data: {                     //要传递的数据
        		        username: function() {
        		            return $("#txtUsername").val();
        		        },
        		        accessid:function() {
        		            return $("#accessid").val();
        		        },
        		    }
                }
            },
            txtIDCardNo:{
            	required: true,
            	checkIDCardNo: true,
            	remote:{
            		url:"/portal/checkIDcard",
            		type:"post",
            		data:{
            			username:function(){
            				return $('#txtUsername').val();
            			},
            			txtIDcardNo:function(){
            				return $('txtIDCardNo').val();
            			},
            		}
            	}
            },
            selUserGroup :{
                required:true
            }
        },
        messages: {
        	txtUsername:{
	    		required: '请填写用户名',
	    		minlength: '用户名最短长度5个字符',
	    		remote:'该用户名已被注册，请重新输入'
	    	},
        	txtFullName: {
                required: '请填写姓名',
                minlength: '姓名不得少于2个字符'
            },
            txtPasswd: {
                required: '请填写密码',
                minlength: '密码最短长度8位',
                remote: '不能使用最近5次使用过的密码'
            },
            txtPasswdConfirm: {
            	equalTo: '两次输入的密码不一致' 
            },
            txtMobile: {
                required: '请填写联系方式',
                isMobile: '请输入正确的手机或者电话号码',
                remote:'该手机号码已被注册'
            },
            txtEmail: {
                email: '请输入正确的邮箱地址',
                remote:'该电子邮箱已被注册'
            },
            accessid: {
            	
            	digits: '只能为数字',
                minlength: '门禁卡号不能少于10位',
                maxlength: '门禁卡号不能超过20位',
                remote:'该门禁卡号被占用'
            },
            txtIDCardNo: {
            	required: '请输入用户身份证号码',
            	remote: '该身份证号码已存在'
            },
            selUserGroup : {
                required:"请选择组"
            }
        },
        errorPlacement: function(error, element) {  
            if (element.is(":checkbox")||element.is(":radio")||element.is("select")){  
                error.appendTo(element.next());  
            }else {                   
                error.insertAfter(element);  
            }  
        }   
    });
	
	$('#btn-submit').click(function(){
    	var bRet = true;
    	if($('#txtUsername').is(':enabled'))
		{
    		bRet = $("#txtUsername").valid() && bRet;
    		bRet = $("#txtPasswd").valid() && bRet;
    		bRet = $("#txtPasswdConfirm").valid() && bRet;
		}
		bRet = $("#txtFullName").valid() && bRet;		
		bRet = $("#txtMobile").valid() && bRet;
		bRet = $("#txtEmail").valid() && bRet;
		bRet = $("#selUserGroup").valid() && bRet;
		
		var btnObj = $(this);
		if (bRet) {
			return true;
		}
		return false;
    });
	
});
